package com.shinewind.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.common.util.FastDFSClientUtil;
import com.shinewind.info.service.IInfoAdminService;
import com.shinewind.pojo.result.Result;
import com.shinewind.pojo.vo.AdminUserListVo;
import com.shinewind.pojo.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Api(value = "后台用户接口",description = "用户模块管理")
public class UserController {
    @DubboReference
    private IInfoAdminService iInfoAdminService;
    @Autowired(required = false)
    private FastDFSClientUtil fastDFSClientUtil;
    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @GetMapping("/user/list")
    @ApiOperation(value = "查询所有用户信息",notes = "用户所有信息",response = Result.class)
    public PageVo<AdminUserListVo> getUserListByCondition(@RequestParam(value = "current",required = true) Integer current,
                                                          @RequestParam(value = "pageSize",required = true) Integer pageSize,
                                                          @RequestParam(value = "number",required = false) String userName,
                                                          @RequestParam(value = "name",required = false) String nickName,
                                                          @RequestParam(value = "contentType",required = false) String role,
                                                          @RequestParam(value = "filterType",required = false) String telePhoneStatus){
        //System.out.println("contentType:"+role);
        PageVo<AdminUserListVo> userListVoList = iInfoAdminService.getUserList(current,pageSize,userName,nickName,role,telePhoneStatus);
        return userListVoList;
    }

    @PostMapping("/user/add")
    @ApiOperation(value = "添加用户信息",notes = "添加用户",response = Result.class)
    public Result addUserInfo(@RequestBody Map<String, Object> data) throws Exception {
        System.out.println("userDto:"+data);
        //将信息写入表中
        Result result = iInfoAdminService.addUser(data);
        return result;
    }

    @PostMapping("/user/avatar/upload")
    @ApiOperation(value = "上传用户头像",notes = "上传用户头像",response = Result.class)
    public Result uploadUserAvatar(MultipartFile file) throws IOException {
        System.out.println("fileName:"+file.getOriginalFilename());
        System.out.println("size:"+file.getSize());
        System.out.println("stream:"+file.getInputStream());
        System.out.println("fileServerUrl:"+fileServerUrl);
        if(file == null){
            throw new RuntimeException("文件不存在");
        }
        else{
            //上传头像
            String fileId = fastDFSClientUtil.uploadFile(file);
            System.out.println("fileId:"+fileId);
            String fileUrl = fileServerUrl + fileId;
            if(StrUtil.isNotBlank(fileUrl)){
                //将用户的头像地址更改
                /*Boolean flag = iUserDetailService.lambdaUpdate().eq(UserDetail::getUId,uId)
                        .set(UserDetail::getAvatar,fileUrl).update();*/
                return Result.success(fileUrl,true,messageConstant.UPLOAD_SUCCESS);
            }
            else{
                return Result.fail(false, messageConstant.UPDATE_ERROR);
            }
        }
    }

    @GetMapping("/user/detail/{uId}")
    @ApiOperation(value = "根据用户id查询用户详细信息",notes = "查询用户详细信息",response = Result.class)
    public Result getUserInfoByUid(@PathVariable("uId") String uId){
        System.out.println("uId:"+uId);
        Result result = iInfoAdminService.getAdminUserDetailByUid(uId);
        return result;
    }

    @PostMapping("/user/update/{uId}")
    @ApiOperation(value = "修改用户信息",notes = "添加用户",response = Result.class)
    public Result updateUserInfo(@PathVariable("uId") String uId,@RequestBody Map<String, Object> data) throws Exception {
        System.out.println("userDto:"+data);
        //将修改的信息写入表中
        Result result = iInfoAdminService.updateUser(uId,data);
        return result;
    }

    @DeleteMapping("/user/delete/{uId}")
    @ApiOperation(value = "删除用户信息",notes = "根据用户id删除用户信息",response = Result.class)
    public Result deleteUserByUid(@PathVariable("uId") String uId){
        Result result = iInfoAdminService.deleteUserByUid(uId);
        return result;
    }
}
